//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * menuinfoDaoHibernate
 *
 * 项目名称:代购系统
 *
 * @author Mengxiangliang
 *
 * 功能描述:
 * 
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * 
 */
package com.gamutsoft.amb.menuinfo.dao.impl;

import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.dao.impl.GenericDaoHibernate;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.menuinfo.dao.IMenuinfoDao;
import com.gamutsoft.amb.menuinfo.model.Menuinfo;

@SuppressWarnings("unchecked")
public class MenuinfoDaoHibernate extends GenericDaoHibernate<Menuinfo, Long> implements IMenuinfoDao {
    
     public MenuinfoDaoHibernate() {
		super(Menuinfo.class);

	}
    
    /**
	 * 根据条件查询记录
	 * @author Mengxiangliang
	 * @param  QueryExample
	 * @return List<Map>
	 * @throws BusinessException
	 */
	@Override
	public List<Map> searchMenuinfoList(QueryExample qe) throws BusinessException {
	
		StringBuffer sb = new StringBuffer();
		  sb.append("SELECT T.ID,T.CODE,T.NAME,T.PARENTCODE,T.URL,T.JUMP_TYPE,T.STATUS,T.CREATE_TIME,T.CREATE_USER，T.img_url,T.order_num FROM MENUINFO T");
		  qe.addOrderBy(" T.order_num desc ");
		return this.findByQueryExample(sb.toString(), qe);
		
	}

	/**
	 * 根据ID查询记录
	 * @author Mengxiangliang
	 * @param id
	 * @return
	 * @throws BusinessException
	 */
	@Override
	public Menuinfo searchMenuinfoById(Long id) throws BusinessException {
	
	  Menuinfo domain = new Menuinfo();
	   String hql = "from Menuinfo where id=?";
	   List<Menuinfo> list = this.getHibernateTemplate().find(hql, id);
	   if(list != null && list.size() >0){
		  domain = list.get(0);
	   }
	   return domain;
		
	}

	/**
	 * 删除记录
	 * @author Mengxiangliang
	 * @param id
	 * @throws BusinessException
	 */
	@Override
	public void deleteMenuinfoById(Long id) throws BusinessException {
	
	 String hql ="delete from Menuinfo where id=?";
	 this.getHibernateTemplate().bulkUpdate(hql, id);
	 
	}

	@Override
	public List<Map> searchMenuinfoByWhere(QueryExample qe, Menuinfo domain)
			throws BusinessException {
		  StringBuffer sb = new StringBuffer();
		  sb.append("SELECT T.ID,T.CODE,T.NAME,T.PARENTCODE,T.URL,T.JUMP_TYPE,T.STATUS,T.CREATE_TIME,T.CREATE_USER,T.img_url,T.order_num FROM MENUINFO T");
		  if(domain!=null){
			  if(domain.getCode()!=null && !"".equals(domain.getCode())){
				  qe.addCondictions("T.CODE","=",domain.getCode());
			  }
			  if(domain.getParentcode()!=null && !"".equals(domain.getParentcode())){
				  qe.addCondictions("T.PARENTCODE","like",domain.getParentcode());
			  }
			  if(domain.getName()!=null && !"".equals(domain.getName())){
				  qe.addCondictions("T.NAME","=",domain.getName());
			  }
			  if(domain.getStatus()!=null && !"".equals(domain.getStatus())){
				  qe.addCondictions("T.STATUS","=",domain.getStatus());
			  }
		  }
		  qe.addOrderBy(" T.order_num desc ");
		  return this.findByQueryExample(sb.toString(), qe);
	}

	@Override
	public void deleteByParentCode(String parentCode) throws BusinessException {
		String hql ="delete from Menuinfo where parentcode=?";
		this.getHibernateTemplate().bulkUpdate(hql, parentCode);
	}

	@Override
	public Menuinfo searchMenuByCode(String code) throws BusinessException {
		 Menuinfo domain = new Menuinfo();
		   String hql = "from Menuinfo where code=?";
		   List<Menuinfo> list = this.getHibernateTemplate().find(hql, code);
		   if(list != null && list.size() >0){
			  domain = list.get(0);
		   }
		   return domain;
	}
	
}